home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * pstile -
- * Repeat copies of a page across the printed sheet
-
- * To compile:
- * cc pstile.c -o pstile
- *
- * Paul Haeberli - 1993
- */
- #include "stdio.h"
- #include "math.h"
- #include "ctype.h"
-
- #define SWIDTH (72.0*8.50) /* sheet width and height */
- #define SHEIGHT (72.0*11.0)
-
- #define INBUFSIZE 4096
- #define MAXPAGES 1000
-
- char buf[INBUFSIZE];
- int secstart[MAXPAGES];
- int secend[MAXPAGES];
- int trailstart;
- int trailend;
- float pdx, pdy, pscale;
- float pwidth, pheight;
-
- indexpages(inf)
- FILE *inf;
- {
- int i, n, before, after;
-
- n = 0;
- trailstart = 0;
- trailend = 0;
- while(fgets(buf,INBUFSIZE,inf)) {
- if(strncmp(buf,"%%Page:",7) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- secstart[n] = after;
- n++;
- } else if(strncmp(buf,"%%Trailer",9) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- trailstart = after;
- trailend = sizeoffile(inf);
- return n;
- }
- before = ftell(inf);
- }
- secend[n-1] = ftell(inf);
- return n;
- }
-
- putpage(inf,pageno,npages)
- FILE *inf;
- int pageno, npages;
- {
- int nbytes;
-
- if(pageno<npages && pageno>=0) {
- nbytes = secend[pageno]-secstart[pageno];
- putsection(inf,secstart[pageno],nbytes);
- }
- }
-
- putsection(inf,start,nbytes)
- FILE *inf;
- int start, nbytes;
- {
- int nr;
-
- fseek(inf,start,0);
- while(nbytes>0) {
- if(!fgets(buf,INBUFSIZE,inf)) {
- fprintf(stderr,"psbook: bad read in putsection\n");
- exit(1);
- }
- nr = strlen(buf);
- nbytes -= nr;
- if(buf[0] != '%')
- fputs(buf,stdout);
- }
- if(nbytes != 0) {
- fprintf(stderr,"psbook: strange read in putsection\n");
- exit(1);
- }
- }
-
- printheader(dosettrans)
- int dosettrans;
- {
- printf("%%!PS-Adobe-2.0\n");
- printf("%%%%EndComments\n");
- printf("/REALshowpage /showpage load def\n");
- printf("/showpage { } def\n");
- if(dosettrans)
- psprintcortab();
- }
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- FILE *inf;
- int npages, dorot, i, dosettrans;
-
- if(argc<2) {
- fprintf(stderr,"\nusage: pstile in.ps [-s pagedx pagedy] [-r] [-nosettrans] > out.ps\n");
- exit(1);
- }
- pwidth = 8.5;
- pheight = 11.0;
- dorot = 0;
- dosettrans = 1;
- for(i=2; i<argc; i++) {
- if(argv[i][0] == '-') {
- switch(argv[i][1]) {
- case 'r':
- dorot = 1;
- break;
- case 's':
- i++;
- pwidth = atof(argv[i]);
- i++;
- pheight = atof(argv[i]);
- break;
- case 'n':
- dosettrans = 0;
- break;
- }
- }
- }
- pwidth *= 72.0;
- pheight *= 72.0;
- psoutfile(stdout);
- printheader(dosettrans);
- inf = fopen(argv[1],"r");
- if(!inf) {
- fprintf(stderr,"psbook: can't open %s\n",argv[1]);
- exit(1);
- }
- npages = indexpages(inf);
- fprintf(stderr,"Document pages %d\n",npages);
- if(npages == 0) {
- fprintf(stderr,"psfold: Document does not have page info\n");
- exit(1);
- }
- putpages(inf,npages,dorot);
- fclose(inf);
- exit(0);
- }
-
- putpages(inf,npages,dorot)
- FILE *inf;
- int npages, dorot;
- {
- int pageno, x, y, nx, ny;
- float cx, cy, dx, dy;
-
- printf("%%%%Page: page%d %d\n",1,1);
- printf("showpage\n");
- if(dorot) {
- dy = pwidth;
- dx = pheight;
- } else {
- dx = pwidth;
- dy = pheight;
- }
- nx = SWIDTH/dx;
- ny = SHEIGHT/dy;
- if(nx<1) nx = 1;
- if(ny<1) ny = 1;
- cx = (SWIDTH-nx*dx)/2.0;
- cy = (SHEIGHT-ny*dy)/2.0;
- for(pageno=0; pageno<npages; pageno++) {
- printf("%%%%Page: page%d %d\n",pageno+2,pageno+2);
- for(y=0; y<ny; y++) {
- for(x=0; x<nx; x++) {
- dosave(inf);
- if(dorot) {
- printf("%f %f translate\n",cx+x*dx+pheight,cy+y*dy);
- printf("90 rotate\n");
- } else {
- printf("%f %f translate\n",cx+x*dx,cy+y*dy);
- }
- printf("%f %f moveto\n",0.0,0.0);
- printf("%f %f lineto\n",pwidth,0.0);
- printf("%f %f lineto\n",pwidth,pheight);
- printf("%f %f lineto\n",0.0,pheight);
- printf("closepath clip newpath\n");
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- }
- }
- }
- printf("REALshowpage\n");
- printf("%%%%EOF\n");
- }
-
- dosave(inf)
- FILE *inf;
- {
- printf("gsave\n");
- }
-
- dorestore(inf)
- FILE *inf;
- {
- if(trailstart)
- putsection(inf,trailstart,trailend-trailstart);
- printf("grestore\n");
- }
-